//
//  GuideViewController.m
//  lncmcc
//
//  Created by 周中广 on 14/12/5.
//  Copyright (c) 2014年 sjyyt. All rights reserved.
//

#import "GuideView.h"

@interface GuideView ()<UIScrollViewDelegate>

@property(nonatomic, strong) UIScrollView  *myScrollView;
@property(nonatomic, strong) UIPageControl *pageControl;

@property(nonatomic, strong) UIImageView * guide1;
@property(nonatomic, strong) UIImageView * guide2;
@property(nonatomic, strong) UIImageView * guide3;
@property(nonatomic, strong) UIButton * button;

@end

@implementation GuideView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        [self addSubview:self.myScrollView];
        
        self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-(100), DEF_SCREEN_WIDTH, (50))];
        //self.pageControl.backgroundColor=[UIColor redColor];
        [self addSubview:self.pageControl];
        
        self.pageControl.currentPageIndicatorTintColor =DEF_RGB_COLOR(56, 173, 255);
        self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        self.pageControl.numberOfPages=3;
        self.pageControl.currentPage=0;
        
        self.button=[[UIButton alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*2, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        [self.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [self.button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
        [self.button setTitle:@"" forState:UIControlStateNormal];
        [self.button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.myScrollView addSubview:self.button];
        
        self.myScrollView.bounces = NO;
        self.myScrollView.pagingEnabled=YES;
        self.myScrollView.showsVerticalScrollIndicator=NO;
        self.myScrollView.showsHorizontalScrollIndicator=NO;
        self.myScrollView.delegate=self;
        [self.myScrollView setContentSize:CGSizeMake(DEF_SCREEN_WIDTH*3, DEF_SCREEN_HEIGHT)];
        
        
        
        for (int i = 1; i <= 3; i++) {
            
            UIImage* middleImage;
            
                middleImage = [UIImage imageNamed:[NSString stringWithFormat:@"Guide%d",i]];
            
            switch (i) {
                case 1:
                {
                    self.guide1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
                    [self.myScrollView addSubview:self.guide1];
                    [self.guide1 setImage:middleImage];
                }
                    break;
                case 2:
                {
                    self.guide2=[[UIImageView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
                    [self.myScrollView addSubview:self.guide2];
                    [self.guide2 setImage:middleImage];
                }
                    break;
                case 3:
                {
                    self.guide3=[[UIImageView alloc]initWithFrame:CGRectMake(DEF_SCREEN_WIDTH*2, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
                    [self.myScrollView addSubview:self.guide3];
                    [self.guide3 setImage:middleImage];
                }
                    break;
                default:
                    break;
            }
        }

    }
    return self;
}

-(void)btnClick{
    self.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    
    int index = fabs(self.myScrollView.contentOffset.x) /DEF_SCREEN_WIDTH;
    self.pageControl.currentPage =index;
    if(self.pageControl.currentPage == 2){
        [self.pageControl setHidden:YES];
    }else{
        [self.pageControl setHidden:NO];
    }
}

@end
